﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DataAccess.DataObject;
using System.Collections.Generic;

namespace EnmineKaitlin.admin
{
    public partial class edit_product : EnmineKaitlin.Common.PageBase
    {
        const string module = "manage-post";
        const int pagesize = 10;
        int page = 1;
        public int totalpage = 0;        
        int CatID = 0;
        string Status = "";
        string LangID = "vn";
        string Name = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CheckPermission(module))
            {
                if (!IsPostBack)
                {
                    lblFunction.Text = "Sản Phẩm";                    
                    GetParam();
                    BindCategory(LangID, CatID.ToString());
                    BindProduct();
                }
            }
            else
            {
                Response.Redirect("~/admin/permission-fail.aspx");
            }
        }
        protected void GetParam()
        {
            if (Request["s"] != null && Request["s"].ToString() != "")
            {
                Name = Request["s"].ToString();
                txtKeyWord.Text = Name;
            }
            if (Request["cat"] != null && Request["cat"].ToString() != "")
            {
                CatID = int.Parse(Request["cat"].ToString());
            }
            if (Request["status"] != null && Request["status"].ToString() != "")
            {
                Status = Request["status"].ToString();
                ddlStatus.SelectedValue = Status;
                ddlStatus.DataBind();
            }
            if (Request["lang"] != null && Request["lang"].ToString() != "")
            {
                LangID = Request["lang"].ToString();
                ddlLanguage.SelectedValue = LangID;
                ddlLanguage.DataBind();
            }
            if (Request["currentpage"] != null && Request["currentpage"].ToString() != "")
            {
                page = int.Parse(Request["currentpage"].ToString());
            }
        }
        protected void ddlLanguage_SelectedIndexChange(object sender, EventArgs e)
        {            
            LangID = ddlLanguage.SelectedValue;
            BindProduct();
        }
        protected void BindProduct()
        {
            IList<OProduct> lstProduct = _BProduct.Get(Name, Status, LangID, CatID, "DESC", "CreateDate", page, pagesize);
            grvProduct.DataSource = lstProduct;
            grvProduct.DataBind();

            if (lstProduct.Count > 0)
            {
                ctlPagging.PageSize = pagesize;
                ctlPagging.ItemCount = (int)lstProduct[0].TotalResult;
                string url = "";                
                if (Name != "") { url += "&s=" + Name; }
                if (CatID != 0) { url += "&cat=" + CatID; }
                if (Status != "") { url += "&status=" + Status; }
                url += "&lang=" + LangID;
                ctlPagging.QueryStringParameterName = url;
                ctlPagging.CurrentIndex = page;
            }
        }
        protected void grvProduct_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            AddCheckBoxAll(e);
        }

        protected void grvProduct_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (_BProduct.Delete(int.Parse(grvProduct.DataKeys[e.RowIndex].Value.ToString())))
            {
                BindProduct();
            }
        }
        protected void btnFilter_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Filter")
            {
                if (txtKeyWord.Text != "Nhập từ khóa" && txtKeyWord.Text != "")
                {
                    Title = txtKeyWord.Text;
                }
                CatID = int.Parse(ddlCategoryFilter.SelectedValue);
                Status = ddlStatus.SelectedValue;
                LangID = ddlLanguage.SelectedValue;
                BindProduct();
            }
        }
        protected void lbtFunction_Click(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "BunkFunction")
            {
                string FunctionName = "";
                if (e.CommandArgument.ToString() == "TopFunction")
                {
                    FunctionName = ddlFunctionTop.SelectedValue;
                }
                else if (e.CommandArgument.ToString() == "BottomFunction")
                {
                    FunctionName = ddlFunctionBottom.SelectedValue;
                }
                switch (FunctionName)
                {
                    case "delete":
                        foreach (GridViewRow gvr in grvProduct.Rows)
                        {
                            if (((CheckBox)gvr.Cells[0].Controls[0]).Checked)
                            {
                                _BProduct.Delete(int.Parse(grvProduct.DataKeys[gvr.RowIndex].Value.ToString()));
                            }
                        }
                        BindProduct();
                        break;
                }

            }
        }
        protected void BindCategory(string LangID, string SelectedValue)
        {
            ddlCategoryFilter.Items.Clear();
            ListItem li = new ListItem("--- Không chọn ---", "0");
            ddlCategoryFilter.Items.Add(li);
            GenCategory(ddlCategoryFilter, 0, -1, LangID);
            ddlCategoryFilter.SelectedValue = SelectedValue;
            ddlCategoryFilter.DataBind();
        }
        protected void GenCategory(DropDownList ddlCategory, int CatParent, int level, string LangID)
        {
            ListItem li;
            String html = "";
            IList<OCategory> lstCategory = _BCategory.Get(0, CatParent, LangID,"catproduct");
            if (lstCategory.Count > 0)
            {
                level += 1;
            }
            for (int i = 0; i < level * 3; i++)
            {
                html += "-";
            }
            foreach (OCategory objCat in lstCategory)
            {
                if (objCat.CatID != HoiDap_CatID)
                {
                    li = new ListItem(html + objCat.CatName, objCat.CatID.ToString());
                    ddlCategory.Items.Add(li);
                    GenCategory(ddlCategory, objCat.CatID, level, LangID);
                }
            }
            ddlCategory.DataBind();
        }
    }
}